package com.miaowang.salad.jobqueue;

import com.alibaba.fastjson.JSON;
import com.miaowang.salad.data.OkHttpResponseData;
import com.miaowang.salad.net.OkHttpUtils;
import com.miaowang.salad.net.UrlUtils;
import com.path.android.jobqueue.Job;
import com.path.android.jobqueue.Params;
import com.path.android.jobqueue.RetryConstraint;

import de.greenrobot.event.EventBus;

/**
 * 根据id获取任务详情
 * @author weihe
 *
 */
public class GetServiceDetailJob  extends Job{

	private String id;
	private int type;   // 0表示请求任务后刷新任务  1 表示任务列表获取任务详情
	public GetServiceDetailJob(String id,int type) {
		super(new Params(JobQueuePriority.PRIORITY_HIGH).groupBy(JobQueuePriority.JOBS_GROUP_USER).requireNetwork());
		this.id = id;
		this.type = type;
	}

	@Override
	public void onAdded() {
		
	}

	@Override
	protected void onCancel() {
		
	}

	@Override
	public void onRun() throws Throwable {
		String url = UrlUtils.GET_SERVICE_REQUESTS_ID +"/"+id;
		String result = OkHttpUtils.get(url);
		OkHttpResponseData msg = JSON.parseObject(result, OkHttpResponseData.class);
		if(type ==0 ){
			EventBus.getDefault().post(new JobEvents.JobSuccess<>(JobEvents.GET_SERVICE_REQUESTS_ID_CURRENT,msg));
		}else{
			EventBus.getDefault().post(new JobEvents.JobSuccess<>(JobEvents.GET_SERVICE_REQUESTS_ID,msg));
		}
	}
	
	@Override
	protected boolean shouldReRunOnThrowable(Throwable throwable) {
		if(type == 0 ){
			EventBus.getDefault().post(new JobEvents.JobFailure(JobEvents.GET_SERVICE_REQUESTS_ID_CURRENT));
		}else{
			EventBus.getDefault().post(new JobEvents.JobFailure(JobEvents.GET_SERVICE_REQUESTS_ID));
		}
		return false;
	}
	
	

}
